package com.jrx.mservices.ribbon.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * 消费服务类
 *
 * @author zwg
 * @create 2018-03-06 17:32
 */
@Service
public class RibbonService {

    public static final Logger logger = LoggerFactory.getLogger(RibbonService.class);

    @Autowired
    private RestTemplate restTemplate;


    @HystrixCommand(fallbackMethod = "callProvideTestFuncFallBackMethod")
    public String callProvideTestFunc(long waitTime){
        String url = "http://provide-eureka/provide/test";
        if(waitTime>0){
            url += "?waitTime="+waitTime;
        }
        String result = restTemplate.getForEntity(url,String.class).getBody();
        logger.info("callProvideTestFunc result:{}",result);
        return result;
    }

    /**
     * callProvideTestFunc 失败后的处理方法
     * @return
     */
    public String callProvideTestFuncFallBackMethod(long waitTime){
        String result = "this is callProvideTestFuncFallBackMethod";
        logger.info("callProvideTestFunc result:{}",result);
        return result;
    }

}
